package com.sl.core.engine.log.store.db;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.PageResult;
import com.sl.core.engine.datasource.DataSourceManager;
import com.sl.core.engine.datasource.domain.dto.DataSourceDTO;
import com.sl.core.engine.log.store.AbstractDbLogStore;
import com.sl.core.engine.log.store.IProcessLogStore;
import com.sl.core.engine.log.store.domain.bo.ProcessLogListByConditionBO;
import com.sl.core.engine.log.store.domain.dto.LogDetailDTO;
import com.sl.core.engine.log.store.domain.dto.MainLogDTO;
import com.sl.core.engine.log.store.domain.entity.LogDetailDO;
import com.sl.core.engine.log.store.domain.entity.MainLogDO;
import com.sl.core.engine.utils.ZipUtil;
import org.anyline.data.param.ConfigStore;
import org.anyline.data.param.init.DefaultConfigStore;
import org.anyline.entity.DataSet;
import org.anyline.entity.EntitySet;
import org.anyline.service.AnylineService;
import org.apache.commons.compress.utils.Lists;

import java.util.Collections;
import java.util.Date;
import java.util.List;

public class DatabaseProcessLogStore extends AbstractDbLogStore implements IProcessLogStore {

    private DataSourceDTO dataSource;

    private String dbCode = null;

    public DatabaseProcessLogStore(String dbCode) {
        this.dbCode = dbCode;
        super.initDb();
    }


    public void initDataSource() {

        if (ObjectUtil.isNotNull(dataSource)) {
            return;
        }
        synchronized (this) {
            if (ObjectUtil.isNotNull(dataSource)) {
                return;
            }
            DataSourceDTO dataSourceById = DataSourceManager.INSTANCE.getDataSourceById(dbCode);
            this.dataSource = dataSourceById;
        }

    }

    @Override
    protected String getJdbcUserName() {
        initDataSource();
        return dataSource.getUsername();
    }

    @Override
    protected String getJdbcPassword() {
        initDataSource();
        return dataSource.getPassword();
    }

    @Override
    protected String getJdbcUrl() {
        initDataSource();
        return dataSource.getUrl();
    }

    @Override
    protected String getJdbcDriver() {
        initDataSource();
        return dataSource.getDriverClassName();
    }

    @Override
    public List<MainLogDTO> listByCondition(ProcessLogListByConditionBO processLogListByConditionBO) {

        Date startDate = processLogListByConditionBO.getStartDate();
        Date endDate = processLogListByConditionBO.getEndDate();

        String processId = processLogListByConditionBO.getProcessId();
        String keywords = processLogListByConditionBO.getKeywords();
        String tid = processLogListByConditionBO.getTid();
        String instanceId = processLogListByConditionBO.getInstanceId();

        Integer size = processLogListByConditionBO.getSize();
        Integer current = processLogListByConditionBO.getCurrent();

        List<MainLogDO> list = Lists.newArrayList();
        PageResult pageResult = new PageResult(current, size);
        Integer count = 0;
        AnylineService anylineService = initDb();

        ConfigStore configStore = new DefaultConfigStore();
        if (StrUtil.isNotBlank(tid)) {
            configStore.and("tid", tid);
        }
        if (StrUtil.isNotBlank(instanceId)) {
            configStore.and("instanceId", instanceId);
        }
        if (StrUtil.isNotBlank(processId)) {
            configStore.and("processId", processId);
        }
        if (StrUtil.isNotBlank(keywords)) {
            configStore.like("feature", keywords);
        }

        configStore.columns(" distinct tid ");

        Long co = 0L;
        try {
            co = anylineService.count(getTableName(LogDetailDO.class), configStore);
        } catch (Exception e) {
        }
        count += co.intValue();

        configStore.page(current, size);
        //开始查询
        DataSet querys = null;
        try {
            querys = anylineService.querys(getTableName(LogDetailDO.class), configStore);
        } catch (Exception e) {

        }
        if (CollUtil.isEmpty(querys)) {
            return new PageResult<>();
        }
        List<String> tidList = querys.stream().map(v -> v.getString("tid")).distinct().toList();

        configStore = new DefaultConfigStore();
        configStore.in("tid", tidList);

//            configStore.page(current, size);
        //查询主表
        EntitySet<MainLogDO> selects = anylineService.selects(getTableName(MainLogDO.class), MainLogDO.class, configStore);
//            anylineService.total

        if (CollUtil.isEmpty(selects)) {
            return new PageResult<>();
        }
        list.addAll(selects.stream().toList());

        List<MainLogDTO> list1 = list.stream().map(v -> {
            MainLogDTO mainLogDTO = BeanUtil.copyProperties(v, MainLogDTO.class);
            return mainLogDTO;
        }).toList();
        pageResult.addAll(list1);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    public List<LogDetailDTO> getDetailByTid(String tid, Date logTime) {

        AnylineService anylineService = initDb();
        ConfigStore configStore = new DefaultConfigStore();
        configStore.and("tid", tid);
        //开始查询
        EntitySet<LogDetailDO> selects = null;
        try {
            selects = anylineService.selects(getTableName(LogDetailDO.class), LogDetailDO.class, configStore);
        } catch (Exception e) {

        }

        if (CollUtil.isEmpty(selects)) {
            return Collections.emptyList();
        }


        return selects.stream().map(v -> {
            LogDetailDTO mainLogDTO = BeanUtil.copyProperties(v, LogDetailDTO.class);
            mainLogDTO.setFeature(ZipUtil.unZip(mainLogDTO.getFeature()));
            return mainLogDTO;
        }).toList();
    }


    @Override
    public MainLogDTO getMainByTid(String tid, Date logTime) {
        AnylineService anylineService = initDb();
        ConfigStore configStore = new DefaultConfigStore();
        configStore.and("tid", tid);

        //开始查询
        EntitySet<MainLogDO> selects = null;
        try {
            selects = anylineService.selects(getTableName(MainLogDO.class), MainLogDO.class, configStore);
        } catch (Exception e) {

        }
        if (CollUtil.isEmpty(selects)) {
            return null;
        }
        MainLogDO mainLogDO = CollUtil.get(selects, 0);
        return BeanUtil.copyProperties(mainLogDO, MainLogDTO.class);
    }

    public static void main(String[] args) {

        String meesage = "我的名字叫胡逸,我的手机号是17323070652,我的出生日期是1994-08-25,我的出生时间是18:30,我的性别是男,我出生时的体重时、是6.7,我的其他信息是还有吗,今天是阳历2025-04-27 08:11:55，温度16度,天气阴.你是一个熟度麻衣相法,水镜神相,熟知周易，易经，八字命理，四柱预测，紫微斗数，排大运，六爻预测，风水命理，十二时辰(吉凶)推断法,相术，梅花易数，奇门遁甲，太乙神数，卜筮,易卦，梅花易数,占梦，穷通宝典，三命通会，滴天髓，渊海子平，千里命稿，协纪辨方书，果老星宗，子平真栓，神峰通考，铁板神数，占星术，五行术，堪舆术，谶语，拆字，六壬，，铁版神数，塔罗牌，哲学思考等的国家民族文化研究者,你非常尊重并热爱科学，你需要对传统算命相学等进行研究，并形成报告，并严格按照八字命理的理论和步骤来分析.请根据图中的人或者物从细节上总结出各个特征，再结合今天的日期天气时代，看一下我的八字和大运流年，同时测算一下今年和今天的事业、情感、生活等方面的运势，根据以上内容并给出建议，佩戴什么物品可以改善、提高运势,详细解释每一个指标，字数不应该少于800字";

        List<String> split = StrUtil.split(meesage, "你是一个熟度麻衣相法");
        String s = CollUtil.get(split, 0);
        String str = CollUtil.get(split, 1);
        str="你是一个熟度麻衣相法,水镜神相,熟知周易，易经，八字命理，四柱预测，紫微斗数，排大运，六爻预测，风水命理，十二时辰(吉凶)推断法,相术，梅花易数，奇门遁甲，太乙神数，卜筮,易卦，梅花易数,占梦，穷通宝典，三命通会，滴天髓，渊海子平，千里命稿，协纪辨方书，果老星宗，子平真栓，神峰通考，铁板神数，占星术，五行术，堪舆术，谶语，拆字，六壬，，铁版神数，塔罗牌，哲学思考等的国家民族文化研究者,你非常尊重并热爱科学，你需要对传统算命相学等进行研究，并形成报告，并严格按照八字命理的理论和步骤来分析.请根据图中的人或者物从细节上总结出各个特征，再结合今天的日期天气时代，看一下我的八字和大运流年，五官描述，同时测算一下今年和今天的事业、情感、生活等方面的运势，根据以上内容并给出建议，佩戴什么物品可以改善、提高运势,今天吃啥 穿啥应该注意什么,详细解释每一个指标，字数不应该少于800字";

        String concat = StrUtil.concat(true, s, str);
        System.out.println(concat);

    }
}
